const db = require('../db/index');
//----------------------------------------------------------------
// 添加新楼栋
exports.addDeviceCate = (req, res) => {
    const cate_name = req.body.cate_name;
    const lou_type = req.body.lou_type;
    const admin_user = req.body.admin_user;
    const phone = req.body.phone;
    // console.log(cate_name);
    // console.log(lou_type);
    // console.log(admin_user);
    // console.log(phone);
    const sql = `select cate_name from lab_device_cate where cate_name = ? and is_delete = 0`
    db.query(sql, cate_name, (err, results) => {
        if (err) return res.cc(err)
        if (results.length > 0) return res.cc("楼栋名称重复请更换重试！")
        const sql = `insert into lab_device_cate (cate_name,lou_type,admin_user,phone) values(?,?,?,?)`
        db.query(sql, [cate_name, lou_type, admin_user, phone], (err, results) => {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('添加楼栋信息失败！')
            res.cc('添加楼栋名称成功！', 0)
        })
    })

}
//----------------------------------------------------------------
// 获取所有分类信息
exports.deviceCateList = (req, res) => {
    const sql = `select * from lab_device_cate where is_delete = 0`
    db.query(sql, (err, results) => {
        if (err) return res.cc(err)
        if (results.length == 0) return res.cc('没有相关信息！')
        // console.log(results);
        res.send({
            status: 0,
            message: '获取分类信息成功！',
            data: results,
        })
    })
}
//----------------------------------------------------------------
// 根据id获取分类名称
exports.getDeviceCate = (req, res) => {
    const id = req.params.id;
    // console.log(id);
    const sql = `select * from lab_device_cate where id=?`
    db.query(sql, id, (err, results) => {
        if (err) return res.cc(err);
        if (results.length === 0) return res.cc('获取分类名称失败！')
        // console.log(results);
        res.send({
            status: 0,
            message: '获取分类名称成功！',
            data: results,
        })
    })
}
//----------------------------------------------------------------
// 根据id编辑楼栋信息
exports.editDeviceCate = (req, res) => {
    const id = req.body.id;
    const cate_name = req.body.cate_name;
    const lou_type = req.body.lou_type;
    const admin_user = req.body.admin_user;
    const phone = req.body.phone;
    // console.log(cate_name);
    // console.log(lou_type);
    // console.log(admin_user);
    // console.log(phone);
    const sql = `select * from lab_device_cate where id<>? and cate_name=? and is_delete=0`
    db.query(sql, [id, cate_name], (err, results) => {
        if (err) return res.cc(err);
        if (results.length === 1) return res.send({
            status: 1,
            message: '楼栋名称重复，请更换后重试！'
        })
        if (results.length === 1 && results[0].cate_name === cate_name) return res.cc('楼栋名称重复，请更换后重试！', 1);
        const sql = `update lab_device_cate set cate_name=?,lou_type=?,admin_user=?,phone=? where id=?`
        db.query(sql, [cate_name, lou_type, admin_user, phone, id], (err, results) => {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('更新楼栋信息失败！')
            res.cc('更新楼栋信息成功！', 0)
        })
    })
}
//----------------------------------------------------------------
// 根据id删除分类名称
exports.deleteDeviceCate = (req, res) => {
    const id = req.params.id;
    // console.log(id);
    const sql = `select * from lab_device_cate where id= ? `
    db.query(sql, id, (err, results) => {
        if (err) return res.cc(err)
        // console.log(results);
        var lou_name = results[0].cate_name
        // console.log(lou_name);
        const sql = `select * from lab_class where lab_address = ? and is_delete = 0 `
        db.query(sql, lou_name, (err, results) => {
            if (err) return res.cc(err)
            if (results.length > 0) return res.cc("该楼栋下已住学生无法删除！")
            // console.log(results.length);
            const sql = `update lab_device_cate set is_delete = 1 where id=?`
            db.query(sql, id, (err, result) => {
                if (err) return res.cc(err)
                if (result.affectedRows !== 1) return res.cc('删除楼栋失败！')
                res.cc('删除楼栋成功！', 0)
            })
        })

    })

}